const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    order: '',
    status: "null",
    page: 1,
    not: false,
    loadingStatus: false,
    afterSale: '',
    timer: ''
  },
  toShop(e){
    wx.navigateTo({
      url: '/pages/shop/shop?id=' + e.target.dataset.id
    })
  },
  //获取订单列表 TODO 分页没有写 每个功能操作之后跳转页面的选择模块需要改
  getOrder(e) {
    let that = this;
    let status = e ? e.target.dataset.id : 10;
    // clearInterval(that.data.timer);
    if (that.data.status == status) { return; }
    that.setData({
      status: "null",
      page: 1,
      not: false,
      loadingStatus: false
    });
    wx.redirectTo({
      url: '/pages/order_list/order_list?id=' + status
    })
    // wx.showLoading({
    //   title: '加载中...'
    // });
    // if (status == 8) {
    //   //获取售后
    //   wx.request({
    //     url: app.buildUrl('/complaint/get-sale-lists'),
    //     header: app.buildHeader(),
    //     method: 'GET',
    //     success(res) {
    //       wx.hideLoading();
    //       if (res.data.code == 200) {
    //         that.setData({
    //           afterSale: res.data.data
    //         })
    //       } else {
    //         app.showToast(res.data.msg)
    //       }
    //     }
    //   });
    // } else {
    //   //获取订单
    //   wx.request({
    //     url: app.buildUrl('/order/get-order'),
    //     header: app.buildHeader(),
    //     method: 'GET',
    //     data: {
    //       status: status
    //     },
    //     success(res) {
    //       wx.hideLoading();
    //       if (res.data.code == 200) {
    //         let unpaid = res.data.data;
    //         that.setData({
    //           order: unpaid
    //         })
    //         that.data.timer = setInterval(() => {
    //           unpaid.forEach(i => {
    //             i.second--;
    //             if (i.second < 0) {
    //               i.minute--;
    //               i.second = 59;
    //             }
    //           });
    //           that.setData({
    //             order: unpaid
    //           })
    //         }, 1000)
    //       } else {
    //         app.showToast(res.data.msg)
    //       }
    //     }
    //   });
    // }
  },
  //取消订单
  cancelOrder(e) {
    let id = e.currentTarget.dataset.id;
    let that = this;
    let status = e.currentTarget.dataset.status;
    if (status == '0') {
      wx.showModal({
        title: '提示',
        content: '是否取消订单',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '请求操作中...',
            });
            that.cancel(id);
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '是否申请退款',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '请求操作中...',
            });
            that.cancel(id);
          }
        }
      })
    }
  },
  //取消订单 申请退款
  cancel(id) {
    let that = this;
    wx.request({
      url: app.buildUrl('/order/cancel-order'),
      header: app.buildHeader(),
      data: {
        order_id: id
      },
      success(res) {
        wx.hideLoading();
        app.showToast(res.data.msg);
        if (res.data.code == 200) {
          setTimeout(() => {
            wx.redirectTo({
              url: `../order_list/order_list?id=${that.data.status}`
            });
          }, 1000)
        }
      }
    });
  },
  //去支付 TODO
  payOrder(e) {
    let that = this;
    wx.request({
      url: app.buildUrl('/pay/pay-order'),
      header: app.buildHeader(),
      data: {
        order_id: e.target.dataset.id
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp + '',
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            success(res) {
              app.showToast('支付成功', 'success')
              wx.redirectTo({
                url: '../success/success?id=' + e.target.dataset.id
              })
            },
            fail(res) { }
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  //查看物流 TODO
  getLogistics: function (e) {
    wx.navigateTo({
      url: '../logistics/logistics?id=' + e.target.dataset.id
    });
  },
  //再次购买
  toActiveDetail: function (e) {
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.target.dataset.id
    });
  },
  //延长收货
  extendOrder: function (e) {
    let order_id = e.target.dataset.id;
    wx.request({
      url: app.buildUrl('/order/extend-order'),
      header: app.buildHeader(),
      method: 'GET',
      data: {
        id: order_id
      },
      success(res) {
        app.showToast(res.data.msg);
        wx.redirectTo({
          url: `../order_list/order_list?id=${that.data.status}`
        });
      }
    });
  },
  //确认收货
  confirmOrder: function (e) {
    let that = this;
    let order_id = e.target.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认要收货吗？',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '收货中...',
          })
          wx.request({
            url: app.buildUrl('/order/confirm-order'),
            header: app.buildHeader(),
            method: "POST",
            data: {
              order_id: order_id
            },
            success(res) {
              wx.hideLoading()
              app.showToast(res.data.msg);
              if (res.data.code == 200) {
                setTimeout(() => {
                  wx.redirectTo({
                    url: `../order_list/order_list?id=${that.data.status}`
                  });
                }, 1000)
              }
            }
          });
        }
      }
    })

  },
  //删除订单
  delOrder: function (e) {
    let that = this;
    let order_id = e.target.dataset.id;
    wx.showModal({
      title: '提示',
      content: '删除后订单后不可恢复',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.buildUrl('/order/del-order'),
            header: app.buildHeader(),
            method: 'GET',
            data: {
              order_id: order_id
            },
            success(res) {
              app.showToast(res.data.msg);
              if (res.data.code == 200) {
                setTimeout(() => {
                  wx.redirectTo({
                    url: `../order_list/order_list?id=${that.data.status}`
                  });
                }, 1000)
              }
            }
          });
        }
      }
    })
  },
  //提醒发货
  remind() {
    wx.showLoading({
      title: '操作请求中...',
    })
    setTimeout(() => {
      wx.hideLoading();
      app.showToast('提醒发货成功')
    }, 1000)
  },
  //售后查看详情
  lookup(e) {
    let id = e.target.dataset.id;
    wx.navigateTo({
      url: '/pages/after_sale/after_sale?id=' + id,
    })
  },
  // 加载更多
  load() {
    let that = this;
    let page = that.data.page;
    let status = that.data.status;
    if (that.data.loadStatus) { return; }
    if (that.data.not) { return; }
    that.setData({
      loadStatus: true
    })
    page = ++page;
    wx.showLoading({
      title: '加载更多...',
    })
    clearInterval(that.data.timer)
    if (status == 8) {
      wx.request({
        url: app.buildUrl('/complaint/get-sale-lists'),
        header: app.buildHeader(),
        data: {
          page: page
        },
        success(res) {
          wx.hideLoading()
          if (res.data.code == 200) {
            if (res.data.data.length == 0) {
              app.showToast('没有更多了')
              that.setData({
                not: true
              });
            } else {
              that.setData({
                afterSale: that.data.afterSale.concat(res.data.data),
                page: page
              });
            }
          } else {
            app.showToast(res.data.msg)
          }
          that.setData({
            loadStatus: false
          })
        }
      });
    } else {
      wx.request({
        url: app.buildUrl('/order/get-order'),
        header: app.buildHeader(),
        method: 'GET',
        data: {
          status: status,
          page: page
        },
        success(res) {
          wx.hideLoading()
          if (res.data.code == 200) {
            if (res.data.data.length == 0) {
              app.showToast('没有更多了')
              that.setData({
                not: true
              });
            } else {
              // let newList = res.data.data;
              that.setData({
                // order: that.data.order.concat(res.data.data),
                page: page
              });
              // if (that.data.status == 0 || that.data.status == 10) {
              //   console.log("xxx")
              //   that.data.order.forEach(i => {
              //     if (true) {
              //       clearTimeout(i.timer);
              //       i.timer = null;
              //       i.timer = setInterval(() => {
              //         let now = new Date().getTime();
              //         let endDate = (i.created_time * 1000) + 900000;
              //         let time = endDate - now;
              //         let m = addZero(parseInt(time / 1000 / 60 % 60));
              //         let s = addZero(parseInt(time / 1000 % 60));
              //         i.showCountDown = `请在00:${m}:${s}内支付`;
              //         if (time <= 0) {
              //           clearTimeout(i.timer);
              //           i.timer = null;
              //           i.showCountDown = `请在00:00:00内支付`;
              //         }
              //         // console.log(that.data.order.concat(newList))
              //         that.setData({
              //           order: that.data.order
              //         })
              //       }, 1000)
              //     }
              //   })
              // } else {
              //   that.setData({
              //     order: that.data.order.concat(newList)
              //   })
              // }
              // function addZero(num) {
              //   return num < 10 ? "0" + num : num;
              // }


              let unpaid = that.data.order.concat(res.data.data)
              that.setData({
                order: unpaid
              })
              that.data.timer = setInterval(() => {
                unpaid.forEach(i => {
                  i.second--;
                  if (i.second < 0) {
                    i.minute--;
                    i.second = 59;
                  }
                });
                that.setData({
                  order: unpaid
                })
              }, 1000)
              
            }
          } else {
            app.showToast(res.data.msg)
          }
          that.setData({
            loadStatus: false
          })
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      status: options.id || 10
    });
    wx.showLoading({
      title: '加载中...',
    })
    if (options.id == 8) { //退款/售后
      wx.request({
        url: app.buildUrl('/complaint/get-sale-lists'),
        header: app.buildHeader(),
        method: 'GET',
        success(res) {
          wx.hideLoading();
          if (res.data.code == 200) {
            that.setData({
              afterSale: res.data.data
            })
          } else {
            app.showToast(res.data.msg)
          }
        }
      });
    } else {
      //获取订单
      wx.request({
        url: app.buildUrl('/order/get-order'),
        header: app.buildHeader(),
        method: 'GET',
        data: {
          status: that.data.status
        },
        success(res) {
          wx.hideLoading();
          if (res.data.code == 200) {
            let unpaid = res.data.data;
            // unpaid.forEach(i => {
            //   i.showCountDown = `请在00:15:00内支付`
            // })
            // that.setData({
            //   order: unpaid
            // })
            // if (that.data.status == 0 || that.data.status == 10){
            //   unpaid.forEach(i=>{
            //     clearTimeout(i.timer);
            //     i.timer = null;
            //     if (i.status == "待付款") {
            //       i.timers = setInterval(() => {
            //         let now = new Date().getTime();
            //         let endDate = (i.created_time * 1000) + 900000;
            //         let time = endDate - now;
            //         let m = addZero(parseInt(time / 1000 / 60 % 60));
            //         let s = addZero(parseInt(time / 1000 % 60));
            //         i.showCountDown = `请在00:${m}:${s}内支付`;
            //         if (time <= 0) {
            //           clearTimeout(i.timers);
            //           i.timers = null;
            //           i.showCountDown = `请在00:00:00内支付`;
            //         }
            //         that.setData({
            //           order: unpaid
            //         })
            //       }, 1000)
            //     }
            //   })
            // }else{
            //   that.setData({
            //     order: unpaid
            //   })
            // }
            // function addZero(num) {
            //   return num < 10 ? "0" + num : num;
            // }


            that.setData({
              order: unpaid
            })
            that.data.timer = setInterval(() => {
              unpaid.forEach(i => {
                i.second--;
                if (i.second < 0) {
                  i.minute--;
                  i.second = 59;
                }
              });
              that.setData({
                order: unpaid
              })
            }, 1000)
          } else {
            app.showToast(res.data.msg)
          }
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})